﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data;
using webapi.Entity;
using webapi.Models;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace webapi.Controllers
{
    public class SetUpCourseController : ApiController
    {
      //获取全部课程
      [HttpGet]
      public IHttpActionResult getAllCourseId()
      {
        List<idAndName> list = new List<idAndName>();
        DataRowCollection dr = CourseService.getAllCourse();
        int len = dr.Count;
        for (int i = 0; i < len; i++)
        {
          list.Add(new idAndName()
          {
            course_id = dr[i][1].ToString(),
            name = dr[i][2].ToString()
          });
        }
        return Json<dynamic>(new { code=0, count=len, data=list });
      }

      //添加开设的课程
      [HttpPost]
      public IHttpActionResult addSetUpCourse([FromBody] JObject obj)
      {
        SetUpCourse setUp = JsonConvert.DeserializeObject<SetUpCourse>(obj["setUpCourse"].ToString());
        int i = SetUpCourseService.addSetUpCourse(setUp);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code = 0, msg, i, setUp });
      }

      //删除开设课程
      [HttpPost]
      public IHttpActionResult deleteSetUpCourse([FromBody] JObject obj)
      {
        List<string> list = JsonConvert.DeserializeObject<List<string>>(obj["setUpCourse"].ToString());
        int len = list.Count;
        Boolean msg = false;
        for (int i = 0; i < len; i++)
        {
          msg = SetUpCourseService.deleteSetUpCourse(list[i]) != -1;
        }
        return Json<dynamic>(new { code = 0, msg, });
      }

      //修改开设课程信息
      [HttpPost]
      public IHttpActionResult updateSetUpCourse([FromBody] JObject obj)
      {
        SetUpCourse setUp = JsonConvert.DeserializeObject<SetUpCourse>(obj["setUpCourse"].ToString());
        int i = SetUpCourseService.updateSetUpCourse(setUp);
        Boolean msg = i != -1 ? true : false;
        return Json<dynamic>(new { code=0, msg, i});
      }

      //获取开设课程信息
      [HttpGet]
      public IHttpActionResult getSetUpCourse(int curr, int limit)
      {
        List<SetUpCourse> setUps = new List<SetUpCourse>();
        DataRowCollection dr = SetUpCourseService.getAllSetUp();
        int j = curr - 1;
        int count = dr.Count;
        int len = count;
        if (count > limit)
        {
          int z = count - (j * limit);
          len = z >= limit ? limit : z;
        }
        for (int i = j*limit; i < len + j*limit; i++)
        {
          setUps.Add(new SetUpCourse()
          {
            name = dr[i][0].ToString(),
            course_id = dr[i][1].ToString(),
            class_course_id = dr[i][2].ToString(),
            point = (double)dr[i][3],
            day = (int)dr[i][4],
            time = (int)dr[i][5],
            start = (int)dr[i][6],
            last = (int)dr[i][7],
            count = int.Parse("0" + dr[i][8]),
            choose = SetUpCourseService.getSelectCountById(dr[i][2].ToString())
          });
        }
        return Json<dynamic>(new { code=0, count, data=setUps });
      }

      //搜索开设的课程
      [HttpGet]
      public IHttpActionResult searchByName(string name)
      {
        List<SetUpCourse> setUps = new List<SetUpCourse>();
        DataRowCollection dr = SetUpCourseService.searchByName(name);
        int len = dr.Count;
        for (int i = 0; i < len; i++)
        {
          setUps.Add(new SetUpCourse()
          {
            name = dr[i][0].ToString(),
            course_id = dr[i][1].ToString(),
            class_course_id = dr[i][2].ToString(),
            point = (double)dr[i][3],
            day = (int)dr[i][4],
            time = (int)dr[i][5],
            start = (int)dr[i][6],
            last = (int)dr[i][7],
            count = int.Parse("0" + dr[i][8]),
            choose = SetUpCourseService.getSelectCountById(dr[i][2].ToString())
          });
        }
        return Json<dynamic>(new { code = 0, count=len, data = setUps });
      }
    }
}
